package com.huilian.iotbox.data.service.impl;

import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dao.CommodityPurchaseRestrictionCommonDao;
import com.huilian.iotbox.data.dao.CommodityPurchaseRestrictionRecordCommonDao;
import com.huilian.iotbox.data.dto.CommodityPurchaseRestrictionDto;
import com.huilian.iotbox.data.dto.CommodityPurchaseRestrictionRecordDto;
import com.huilian.iotbox.data.enums.CommodityPurchaseRestrictionRecordStatusEnum;
import com.huilian.iotbox.data.enums.CommodityPurchaseRestrictionStatusEnum;
import com.huilian.iotbox.data.exception.SysException;
import com.huilian.iotbox.data.service.CommodityPurchaseRestrictionCommonService;
import com.huilian.iotbox.data.vo.CommodityPurchaseRestrictionVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @author LinYinDong
 * @date 2024/1/22 13:45
 */
@Service
public class CommodityPurchaseRestrictionCommonServiceImpl implements CommodityPurchaseRestrictionCommonService {
    @Resource
    private CommodityPurchaseRestrictionCommonDao commodityPurchaseRestrictionCommonDao;
    @Resource
    private CommodityPurchaseRestrictionRecordCommonDao commodityPurchaseRestrictionRecordCommonDao;

    /**
     * 必须传参数 <br/>
     * commodityStoreUpDownId 商品id  <br/>
     * memberId 会员id  <br/>
     * quantity 购买套餐数量  <br/>
     * commodityName 套餐名字（非必填）  <br/>
     */
    @Override
    public ServerResponse checkCommodityPurchaseRestrictionBuy(CommodityPurchaseRestrictionDto commodityPurchaseRestrictionDto) {
        if (StringUtils.isEmpty(commodityPurchaseRestrictionDto.getCommodityStoreUpDownId())) {
            throw new SysException("商品Id不能为空");
        }
        if (StringUtils.isEmpty(commodityPurchaseRestrictionDto.getMemberId())) {
            throw new SysException("会员Id不能为空");
        }
        if (StringUtils.isEmpty(commodityPurchaseRestrictionDto.getQuantity())) {
            throw new SysException("购买数量不能为空");
        }
        commodityPurchaseRestrictionDto.setStatus(CommodityPurchaseRestrictionStatusEnum.NORMAL.getCode());
        CommodityPurchaseRestrictionVo oneCommodityPurchaseRestriction = commodityPurchaseRestrictionCommonDao.findOneCommodityPurchaseRestriction(commodityPurchaseRestrictionDto);
        if (!StringUtils.isEmpty(oneCommodityPurchaseRestriction)) {
            //查询会员购买的套餐次数
            CommodityPurchaseRestrictionRecordDto commodityPurchaseRestrictionRecordDto = new CommodityPurchaseRestrictionRecordDto();
            commodityPurchaseRestrictionRecordDto.setMemberId(commodityPurchaseRestrictionDto.getMemberId());
            commodityPurchaseRestrictionRecordDto.setCommodityPurchaseRestrictionId(oneCommodityPurchaseRestriction.getId());
            commodityPurchaseRestrictionRecordDto.setStatus(CommodityPurchaseRestrictionRecordStatusEnum.NORMAL.getCode());
            // 已购买数量
            final Integer quantityPurchased = commodityPurchaseRestrictionRecordCommonDao.getCommodityPurchaseRestrictionRecordByMemberIdCount(commodityPurchaseRestrictionRecordDto);

            if (quantityPurchased == 0) {
                // 从来没买过
                if (commodityPurchaseRestrictionDto.getQuantity() > oneCommodityPurchaseRestriction.getPurchaseLimit()) {
                    if (ObjectUtils.isEmpty(commodityPurchaseRestrictionDto.getCommodityName())) {
                        throw new SysException(String.format("很抱歉，您本次购买的商品数量已超过限购数量，剩余可购买数量%s个", oneCommodityPurchaseRestriction.getPurchaseLimit()));
                    } else {
                        throw new SysException(String.format("很抱歉，您本次购买的商品（" + commodityPurchaseRestrictionDto.getCommodityName() + "）数量已超过限购数量，剩余可购买数量%s个", oneCommodityPurchaseRestriction.getPurchaseLimit()));
                    }
                }
            } else {
                //判断购买是否大于限购
                final Integer remainingPurchaseQuantity = oneCommodityPurchaseRestriction.getPurchaseLimit() - quantityPurchased;

                if (quantityPurchased >= oneCommodityPurchaseRestriction.getPurchaseLimit()) {
                    // 增购时购已买数量已超
                    if (ObjectUtils.isEmpty(commodityPurchaseRestrictionDto.getCommodityName())) {
                        throw new SysException(String.format("很抱歉，您已参与购买%s个本限购套餐，剩余可购买0个", quantityPurchased));
                    } else {
                        throw new SysException(String.format("很抱歉，您已参与购买%s个本限购套餐（" + commodityPurchaseRestrictionDto.getCommodityName() + "），剩余可购买0个", quantityPurchased));
                    }
                } else if ((commodityPurchaseRestrictionDto.getQuantity() + quantityPurchased) > oneCommodityPurchaseRestriction.getPurchaseLimit()) {
                    // 增购时已购买数量未超，但增购数量超了
                    if (ObjectUtils.isEmpty(commodityPurchaseRestrictionDto.getCommodityName())) {
                        throw new SysException(String.format("很抱歉，您本次参与增购限购套餐的数量已超出可购买个数，剩余可购买%s个", remainingPurchaseQuantity));
                    } else {
                        throw new SysException(String.format("很抱歉，您本次参与增购限购套餐（" + commodityPurchaseRestrictionDto.getCommodityName() + "）的数量已超出可购买个数，剩余可购买%s个", remainingPurchaseQuantity));
                    }
                }
            }
        }
        return ServerResponse.createBySuccess();
    }
}
